package com.app.controller;

import com.app.model.PolicyRequireModel;
import com.app.service.PolicyRequireService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.vo.PolicyRequireVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 政策规定
 */
@RestController
@Controller
@RequestMapping("/policRequire")
public class PolicyRequireController {
    @Autowired
    private PolicyRequireService policyRequireService;

    // 查询列表
    @GetMapping("/getPolicyRequireList")
    public Result getPolicyRequireList(@RequestParam(defaultValue = "0") Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit) {
        PolicyRequireVo policyRequireList = policyRequireService.getPolicyRequireList(page, limit);
        return Result.success(policyRequireList);
    }

    //详情
    @GetMapping("/getPolicyRequireById")
    public Result getPolicyRequireById(Integer id) {
        PolicyRequireModel PolicyRequire = policyRequireService.getById(id);
        return Result.success(PolicyRequire);
    }

    //根据类型查
    @GetMapping("/getPolicyRequireByType")
    public Result getPolicyRequireByType(@RequestParam(defaultValue = "0") Integer page,
                                         @RequestParam(defaultValue = "10") Integer limit, Integer type) {
        PolicyRequireVo policyRequireVo = policyRequireService.getByType(type, page, limit);
        return Result.success(policyRequireVo);
    }

    //新增和修改
    @PostMapping("/addAndUpdatePolicyRequire")
    public Result addAndUpdatePolicyRequire(@RequestBody PolicyRequireModel policyRequireModel) {
        PolicyRequireModel policyRequire = policyRequireService.getById(policyRequireModel.getId());
        if (policyRequire != null) {
            policyRequireModel.setUpdateTime(CommonHelp.getNowTimestamp());
            policyRequireService.updateById(policyRequireModel);
            return Result.success("");
        } else {
            policyRequireModel.setCreateTime(CommonHelp.getNowTimestamp());
            policyRequireService.save(policyRequireModel);
            return Result.success("");
        }
    }

    //删除
    @GetMapping("/delPolicyRequire")
    public Result delPolicyRequire(Integer id) {
        policyRequireService.removeById(id);
        return Result.success("");
    }
}
